//
//  GiftDetailViewController.m
//  礼物
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "GiftDetailViewController.h"
@interface GiftDetailViewController ()

@end

@implementation GiftDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.model.short_title isEqualToString:@""]) {
        self.navigationItem.title=self.model.title;
    }else {
        self.navigationItem.title=self.model.short_title;
    }
     self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fenxiang"] style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemAction:)];

    self.webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-49)];
    NSString *str=self.model.content_url;
    NSURL *url=[NSURL URLWithString:str];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    self.webView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.webView];
    self.webView.scrollView.delegate=self;
    self.webView.scrollView.userInteractionEnabled=YES;
    self.webView.scrollView.contentInset=UIEdgeInsetsMake(200.0*WIDTH/375, 0, 0, 0);
    //相对于0点,已经向下偏移了-200
    //相对于0点,图片坐标应该是(0,-200)
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200.0*WIDTH/375, self.view.frame.size.width, 200*HEIGHT/667)];
    self.myImageView.backgroundColor=[UIColor whiteColor];
    [self.myImageView sd_setImageWithURL:[NSURL URLWithString:self.model.cover_image_url]];
    //设置imageView高度改变时宽度也跟着改变
    self.myImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.webView.scrollView addSubview:self.myImageView];
    

    [self.webView setOpaque:NO];//设置控件是否透明
    
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, -50.0*HEIGHT/667, WIDTH, 50*HEIGHT/667)];
    [self.webView.scrollView addSubview:titleLabel];
    titleLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    titleLabel.text=[NSString stringWithFormat:@"    %@",self.model.title];
    [titleLabel setFont:[UIFont systemFontOfSize:17]];
    
    
    UIView *aView=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-49,WIDTH, 49)];
    [self.view addSubview:aView];
    [UMSocialConfig setSupportedInterfaceOrientations:UIInterfaceOrientationMaskAll];
    
    self.collectingButton=[UIButton buttonWithType:UIButtonTypeCustom];
    self.collectingButton.frame=CGRectMake(aView.frame.size.width-54, 5, 34, 34) ;
    [aView addSubview:self.collectingButton];
    NSString *str1=[[NSString alloc]init];
    NSMutableArray *array1=[[DataBaseHandle shareDataBaseHandle]select];
    for (Model *model1 in array1) {
        if ([model1.content_url isEqual:self.model.content_url]) {
            str1=model1.content_url;
        }
    }
    if(![str1 isEqualToString:@""])
    {
        [self.collectingButton setImage:[UIImage imageNamed:@"iconfont-shoucang-2.png"] forState:UIControlStateNormal];
    }
    else
    {
        [self.collectingButton setImage:[UIImage imageNamed:@"iconfont-shoucang.png"] forState:UIControlStateNormal];
    }
    [self.collectingButton addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)ButtonAction:(UIButton *)button
{
    
    NSString *str=[[NSString alloc]init];
    NSMutableArray *array=[[DataBaseHandle shareDataBaseHandle]select];
    for (Model *model in array) {
        if ([model.content_url isEqual:self.model.content_url]) {
            str=model.content_url;
        }
    }
    if(![str isEqualToString:@""])
    {
        [[DataBaseHandle shareDataBaseHandle]deleteTable:self.model.content_url];
        [self.collectingButton setImage:[UIImage imageNamed:@"iconfont-shoucang.png"] forState:UIControlStateNormal];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:取消收藏" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }
    else
    {
        [[DataBaseHandle shareDataBaseHandle]insertCellModelTable:self.model];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收藏成功" message:@"恭喜你收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [self.collectingButton setImage:[UIImage imageNamed:@"iconfont-shoucang-2.png"] forState:UIControlStateNormal];
        
    }
    
    
    
}
-(void)leftBarButtonItemAction:(UIBarButtonItem *)barButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)rightBarButtonItemAction:(UIBarButtonItem *)barButtonItem
{
    
    
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:self.model.cover_image_url]];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"507fcab25270157b37000010"
                                      shareText:[NSString stringWithFormat:@"亲爱的小伙伴们，给你们一个好东西哦，快来看一看哈！%@",self.model.content_url]
                                     shareImage:[UIImage imageWithData:data]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToWechatSession,nil]
                                       delegate:nil];
    
}













-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView==self.webView.scrollView) {
        CGFloat y = scrollView.contentOffset.y + 64;//加上导航栏高度,第一次是-200
        if (y < -200*HEIGHT/667) {
            CGRect frame = self.myImageView.frame;
            frame.origin.y = y;//imageView的frame是不断往上偏移
            frame.size.height =  -y;//tablview向下偏移了多少,高度就增加多少
            self.myImageView.frame = frame;
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
